package com.xiaotou.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaotou.db.dao.CommonDAO;
import com.xiaotou.db.pojo.WbUser;
import com.xiaotou.domain.Engine;
import com.xiaotou.domain.Prey;
import com.xiaotou.weibo.WeiboClient;
import com.xiaotou.weibo.WeiboClientException;
import com.xiaotou.weibo.WeiboFactory;
import com.xiaotou.weibo.WeiboUser;

@Service
public class PreyService {

	private CommonDAO commonDao;

	private Engine engine;

	public List<Prey> getRandomPreys(final WbUser loginUser)
			throws WeiboClientException {
		final WeiboClient sinaWeiboClient = WeiboFactory
				.createWeiboClient(loginUser);
		final List<Prey> randomUsers = engine.actPreys(sinaWeiboClient
				.getRandomUsers());
		return randomUsers;
	}

	@Transactional(readOnly = true)
	public List<WeiboUser> getRandomPreysFromDb() {
		final List<WbUser> wbUsers = this.commonDao
				.findByNamedQuery(WbUser.FIND_ALL);
		final List<WeiboUser> randomUsers = new ArrayList<WeiboUser>();
		for (WbUser wbUser : wbUsers) {
			randomUsers.add(WeiboFactory.createWeiboUser(wbUser));
		}
		return randomUsers;
	}

	public void setCommonDao(CommonDAO commonDao) {
		this.commonDao = commonDao;
	}

	public void setEngine(Engine engine) {
		this.engine = engine;
	}

}